# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: xiaobien_token_ls.py
# Time: 14:20

import requests
import sys
import os

sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(""))))
from common.tools.cryptogram_ls import BaseEncryptionDecryption
from common.utils.read_util import OperationJson, OperationIni


class GetTheTokenParameter():
    def __init__(self):
        self.headers = OperationJson().get_own_json("user_test.json").get("login")
        self.data = OperationIni("usr.ini").get_ini_file_path_section_data("account")
        self.url = OperationIni("setting.ini").get_ini_file_path_section_data("host").get(
            "xiaobien") + "/account/v1/login"

    def access_cookie_token(self):
        data = {
            "account": self.data.get("username"),
            "password": self.data.get("password")
        }
        response = requests.request(method="post", url=self.url, headers=self.headers,
                                    json=data).json()
        token = response.get("token")
        userid = response.get("user_id")
        combination = f"{userid}:{token}"
        cookie = f"HBSID={token}"
        authorization = f"HBAPI {BaseEncryptionDecryption().get_encryption(combination)}"
        return cookie, authorization
